
package com.shop.cloud.mall.api.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.mall.api.mapper.VipUserRechargeMapper;
import com.shop.cloud.mall.api.service.VipCostService;
import com.shop.cloud.mall.api.service.VipUserRechargeService;
import com.shop.cloud.mall.api.service.VipUserService;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.VipCost;
import com.shop.cloud.mall.common.entity.VipUser;
import com.shop.cloud.mall.common.entity.VipUserRecharge;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 用户会员充值
 *
 * @author
 * @date 2024-03-19 15:24:20
 */
@Slf4j
@Service
@AllArgsConstructor
public class VipUserRechargeServiceImpl extends ServiceImpl<VipUserRechargeMapper, VipUserRecharge> implements VipUserRechargeService {

    private final VipCostService vipCostService;
    private final VipUserService vipUserService;

    @Override
    public IPage<VipUserRecharge> page2(IPage<VipUserRecharge> page, VipUserRecharge vipUserRecharge) {
        return baseMapper.selectPage2(page, vipUserRecharge);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Klock(name = "notifyVipUserRecharge", keys = {"#vipUserRecharge.getId()"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    public void notifyVipUserRecharge(VipUserRecharge vipUserRecharge) {
        if(CommonConstants.NO.equals(vipUserRecharge.getIsPay())){
            vipUserRecharge.setIsPay(CommonConstants.YES);
            //获取用户当前会员信息
            VipUser vipUser = vipUserService.getOne(Wrappers.<VipUser>lambdaQuery().eq(VipUser::getUserId, vipUserRecharge.getUserId()));
            //如果用户已是有效会员，并不更换会员类型，则视为续费，此条充值的开始时间为当前用户会员的结束时间
            if(vipUser != null && vipUser.getEndTime().isAfter(LocalDateTime.now()) && vipUser.getVipTypeId().equals(vipUserRecharge.getVipTypeId())){
                vipUserRecharge.setBeginTime(vipUser.getEndTime());
            }else{
                vipUserRecharge.setBeginTime(vipUserRecharge.getPaymentTime());
            }
            VipCost vipCost = vipCostService.getById(vipUserRecharge.getVipCostId());
            if(MallConstants.VIP_COST_TYPE_1.equals(vipCost.getType())){
                vipUserRecharge.setEndTime(vipUserRecharge.getBeginTime().plusDays(vipUserRecharge.getCount()));
            }else if(MallConstants.VIP_COST_TYPE_2.equals(vipCost.getType())){
                vipUserRecharge.setEndTime(vipUserRecharge.getBeginTime().plusMonths(vipUserRecharge.getCount()));
            }else if(MallConstants.VIP_COST_TYPE_3.equals(vipCost.getType())){
                vipUserRecharge.setEndTime(vipUserRecharge.getBeginTime().plusYears(vipUserRecharge.getCount()));
            }
            this.updateById(vipUserRecharge);
            //新增或更新会员信息
            if(vipUser == null){
                vipUser = new VipUser();
                vipUser.setUserId(vipUserRecharge.getUserId());
            }
            vipUser.setVipTypeId(vipUserRecharge.getVipTypeId());
            vipUser.setEndTime(vipUserRecharge.getEndTime());
            vipUserService.saveOrUpdate(vipUser);
        }
    }
}
